import pygame

from actor.xiao_tie import XiaoTie
from game import SCREEN_WIDTH, SCREEN_HEIGHT, GAME_TITLE
from sence.tunnel_war_scene import TunnelWarScene


class TunnelWarGame:
    def __init__(self):
        pygame.init()
        # 获取游戏时钟
        self.clock = pygame.time.Clock()
        # 创建游戏的窗口 640 * 236 根据要显示图片的大小设置
        self.surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
        pygame.display.set_caption(GAME_TITLE)
        self.xiao_tie = XiaoTie(0, 0)
        self.current_scene = TunnelWarScene(self.xiao_tie)

    def run(self):
        while True:
            # 游戏循环
            down_flag = False
            for event in pygame.event.get():
                # 关闭事件，进行退出处理
                if event.type == pygame.QUIT:
                    exit()
                elif event.type == pygame.KEYDOWN:
                    down_flag = True
            key_list = pygame.key.get_pressed()
            # 1.获取场景的视窗图像
            current_surface = self.current_scene.get_current_surface()
            # 2.绘制窗口
            self.surface.blit(current_surface, (0, 0))
            # 3.场景的运动
            self.current_scene.run(down_flag, key_list)

            # 通过时钟对象指定循环频
            self.clock.tick(20)
            # 调用flip方法更新显示,也可以使用update方法
            pygame.display.flip()